$(function () {
    var accident_datagridNOT = $("#accident_datagridNOT");
    var accident_datagridYES = $("#accident_datagridYES");
    var accident_dialog = $("#accident_dialog");
    var accident_dialog2 = $("#accident_dialog2");
    var accident_form = $("#accident_form");
    var accident_form2 = $("#accident_form2");
    var accident_combogrid = $("#accident_combogrid");
    var insure_combogrid = $("#insure_combogrid");
    var qo = $("#qo").val();
    var claimState;
    var insurerId;

    // CLAIM_STATE_UNVERIFIED为等待验证用户,未证实用户,表示用户已报案但并未验证
    const CLAIM_STATE_UNVERIFIED = 1;
    // CLAIM_STATE_VERIFIED为已经验证用户,证实用户,表示用户已报案并且验证通过
    const CLAIM_STATE_VERIFIED = 2;
    // CLAIM_STATE_UNINVESTIGATE为查勘未通过,用户将转为废弃单
    const CLAIM_STATE_UNINVESTIGATE = 3;
    // CLAIM_STATE_INVESTIGATE为查勘通过,表示用户已经通过并且进入核价核损界面等待上级审核
    const CLAIM_STATE_INVESTIGATE = 4;
    // CLAIM_STATE_UNAUDIT为审批未通过,当审批未通过时重新转入查勘界面进行查勘
    const CLAIM_STATE_UNAUDIT = 5;
    // CLAIM_STATE_AUDIT为审批通过,当审批通过时将进入理赔支付界面
    const CLAIM_STATE_AUDIT = 6;
    // CLAIM_STATE_PAY为已经赔付
    const CLAIM_STATE_PAY = 7;


    // accident_datagridNOT理赔列表初始化:未验证报案
    accident_datagridNOT.datagrid({
        fit: true,
        url: "/accident/list?state=" + qo,
        fitColumns: true,
        striped: true,
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        pageSize: 10,
        pageList: [3, 5, 10, 15],
        toolbar: "#accident_tbNOT",
        columns: [[
            {field: 'ck', checkbox: true},
            {title: '编号', field: 'id', hidden: true},
            {
                title: '当前状态', field: 'claimState', width: 100, formatter: function (value) {
                    if (value === CLAIM_STATE_UNVERIFIED) {
                        return "<font color='red'>等待验证</font>";
                    } else if (value === CLAIM_STATE_VERIFIED) {
                        return "<font color='#1e90ff'>备案已提交</font>";
                    } else if (value === CLAIM_STATE_UNINVESTIGATE) {
                        return "<font color='#8b0000'>审查未通过,已转废弃单</font>";
                    } else if (value === CLAIM_STATE_INVESTIGATE) {
                        return "<font color='#006400'>正在审核中,请耐心等待</font>";
                    } else if (value === CLAIM_STATE_UNAUDIT) {
                        return "<font color='#006400'>正在审核中,请耐心等待</font>";
                    } else if (value === CLAIM_STATE_AUDIT) {
                        return "<font color='#00008b'>审批通过暂未赔付</font>";
                    } else if (value === CLAIM_STATE_PAY) {
                        return "<font color='green'>已赔付结案</font>";
                    }
                }
            },
            {
                title: '报案人姓名', field: 'name', width: 100, formatter: function (value, row) {
                    if (row.claim) {
                        return row.claim.name;
                    }
                }
            },
            {
                title: '性别', field: 'gender', width: 100, formatter: function (value, row) {
                    if (row.claim) {
                        return row.claim.gender ? "男" : "女";
                    }
                }
            },
            {
                title: '联系方式', field: 'phone', width: 100, formatter: function (value, row) {
                    if (row.claim) {
                        return row.claim.phone;
                    }
                }
            },
            {
                title: '邮箱', field: 'email', width: 100, formatter: function (value, row) {
                    if (row.claim) {
                        return row.claim.email;
                    }
                }
            },
            {title: '发生时间', field: 'accidentTime', width: 100},
            {title: '发生地点', field: 'accidentPlace', width: 100},
            {
                title: '保险单号', field: 'insurancebill', width: 100, formatter: function (value) {
                    return value ? value.oddNumber : "<font color='#cd5c5c'>暂未填保险单</font>";
                }
            },
            {
                title: '录入员工', field: 'investigator', width: 100, formatter: function (value, row) {
                    return value ? value.realname : "<font color='#cd5c5c'>未知录入人</font>";
                }
            }
        ]],
        onSelect: function (index, row) {
            if (row.claimState == 1) {
                $("#tb_edit").show();
                $("#tb_verified").show();
            } else {
                $("#tb_edit").hide();
                $("#tb_verified").hide();
            }
        }

    });

    // accident_datagridNOT:END


    function getempStateName(qo) {
        if (qo == 2) {
            return "录入员工";
        } else if (qo == 3) {
            return "验证员工";
        } else if (qo == 4) {
            return "审核员工";
        }
    }

    var empStateName = getempStateName(qo);

    function getempStateNameinfo(qo) {
        if (qo == 2) {
            return "investigator";
        } else if (qo == 3) {
            return "auditor";
        } else if (qo == 4) {
            return "auditor";
        } else if (qo == 1) {
            return "investigator";
        }
    }

    var empStateNameinfo = getempStateNameinfo(qo);

    // accident_datagridYES理赔列表初始化:已经验证报案
    accident_datagridYES.datagrid({
        fit: true,
        url: "/accident/list?state=" + qo,
        fitColumns: true,
        striped: true,
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        pageSize: 10,
        pageList: [3, 5, 10, 15],
        toolbar: "#accident_tbYES",
        columns: [[
            {field: 'ck', checkbox: true},
            {title: '编号', field: 'id', hidden: true},
            {
                title: '当前状态', field: 'claimState', width: 100, formatter: function (value) {
                    if (value === CLAIM_STATE_UNVERIFIED) {
                        return "<font color='green'>等待验证</font>";
                    } else if (value === CLAIM_STATE_VERIFIED) {
                        return "<font color='#ff4500'>验证查勘</font>";
                    } else if (value === CLAIM_STATE_UNINVESTIGATE) {
                        return "<font color='#8b0000'>查勘未通过,已转废弃单</font>";
                    } else if (value === CLAIM_STATE_INVESTIGATE) {
                        return "<font color='#ff4500'>查勘通过待审核证明</font>";
                    } else if (value === CLAIM_STATE_UNAUDIT) {
                        return "<font color='#ff4500'>审核已驳回,请重新查勘</font>";
                    } else if (value === CLAIM_STATE_AUDIT) {
                        return "<font color='#ff4500'>审批已通过,暂未赔付</font>";
                    } else if (value === CLAIM_STATE_PAY) {
                        return "<font color='green'>已履行赔付</font>";
                    }
                }
            },
            {
                title: '报案人姓名', field: 'name', width: 100, formatter: function (value, row) {
                    if (row.claim) {
                        return row.claim.name;
                    }
                }
            },
            {
                title: '保险单号', field: 'insurancebill', width: 100, formatter: function (value) {
                    return value ? value.oddNumber : "<font color='#cd5c5c'>暂未填保险单</font>";
                }
            },
            {
                title: '联系方式', field: 'phone', width: 100, formatter: function (value, row) {
                    if (row.claim) {
                        return row.claim.phone;
                    }
                }
            },
            {title: '报案时间', field: 'claimTime', width: 100},
            {
                title: empStateName, field: empStateNameinfo, width: 100, formatter: function (value, row) {
                    return value ? value.realname : "<font color='#cd5c5c'>未知录入人</font>";
                }
            },
            {title: '备注', field: 'accidentRemark', width: 100}
        ]],
        onSelect: function (index, row) {
            if (row.claimState == 7) {
                $("#confirmPay").hide();
            } else {
                $("#confirmPay").show();
            }
        }
    });
    // accident_datagridYES:END

    // 下拉框的初始化
    $("#state_combobox").combobox({
        panelHeight: 'auto',
        mode: 'remote',
        valueField: 'value',
        textField: 'text',
        value: '所有状态',
        width: '190',
        data: [{
            text: '所有状态',
            value: '0'
        }, {
            text: '等待验证',
            value: '1'
        }, {
            text: '备案已提交',
            value: '2'
        }, {
            text: '审查未通过',
            value: '3'
        }, {
            text: '正在审核中,请耐心等待',
            value: '5'
        }, {
            text: '审批通过暂未赔付',
            value: '6'
        }, {
            text: '已赔付结案',
            value: '7'
        }],
        onSelect: function (record) {
            console.log(record);
            accident_datagridNOT.datagrid("load", {
                claimState: record.value
            });
        }
        /* loader: function (param) {
             param = valueField;
         }*/
    });
    // 下拉框的初始化:END

    // accident_dialog:添加编辑对话框初始化
    accident_dialog.dialog({
        title: '理赔编辑',
        width: '350px',
        height: '500px',
        buttons: "#dialog_btn",
        closed: true
    });
    // accident_dialog:END

    // accident_dialog2:添加编辑对话框初始化
    accident_dialog2.dialog({
        title: '理赔编辑',
        width: '350px',
        height: '400px',
        buttons: "#dialog_btn2",
        closed: true
    });
    // accident_dialog2:END

    // accident_combogrid:初始化数据表格下拉框
    accident_combogrid.combogrid({
        url: '/client/list?state=2',
        panelWidth: 500,
        panelHeight: 310,
        fitColumns: true,
        editable: false,
        pagination: true,
        rownumbers: true,
        value: 'id',
        idField: 'id',
        textField: 'name',
        striped: true,
        loadMsg: "加载中",
        columns: [[
            {field: 'id', title: '编号', hidden: true},
            {field: 'name', title: '姓名', width: 100},
            {
                field: 'gender', title: '性别', width: 40, formatter: function (value) {
                    return value ? "男" : "女";
                }
            },
            {field: 'phone', title: '联系方式', width: 100},
            {field: 'email', title: '邮箱', width: 120}
        ]]
    });
    // accident_combogrid:初始化数据表格下拉框:END

    // insure_combogrid:初始化数据表格下拉框
    insure_combogrid.combogrid({
        panelWidth: 500,
        panelHeight: 310,
        fitColumns: true,
        editable: false,
        rownumbers: true,
        value: 'id',
        idField: 'id',
        textField: 'oddNumber',
        striped: true,
        columns: [[
            {field: 'id', title: '保单id编号', hidden: true},
            {field: 'oddNumber', title: '保险单号', width: 160},
            {field: 'beginDate', title: '保险生效时间', width: 85},
            {field: 'endDate', title: '保险失效时间', width: 85}
        ]]
    });
    // insure_combogrid:初始化数据表格下拉框:END


    // accident_datagridNOT响应函数集合对象
    var cmdObj = {
        // 理赔添加
        add: function () {
            accident_dialog.dialog("open");
            accident_dialog.dialog("setTitle", "新增备案表");
            accident_form.form("clear");
        },
        // 理赔编辑
        edit: function () {
            var row = accident_datagridNOT.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要编辑的行");
                return;
            }
            //清除以前的数据
            accident_form.form("clear");
            accident_form.form("load", row);
            accident_dialog.dialog("open");
            accident_dialog.dialog("setTitle", "备案表修改");
            accident_combogrid.combogrid("setValue", row.claim.name);

        },
        // rui:修改状态:
        changeclaimstate: function () {
            var row = accident_datagridNOT.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要操作的行");
                return;
            }
            $.messager.confirm('确认', '您确认想要执行该操作吗？', function (r) {
                if (r) {
                    $.get("/accident/changeClaimState?claimState=" + claimState, {id: row.id}, function (data) {
                        if (data.succeed) {
                            $.messager.alert("温馨提示", "操作成功");
                            accident_datagridNOT.datagrid("load");
                        } else {
                            $.messager.alert("温馨提示", data.errorMsg);
                        }
                    })
                }
            });
        },

        changeclaimstate2: function () {
            var row = accident_datagridYES.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要操作的行");
                return;
            }
            $.messager.confirm('确认', '您确认想要执行该操作吗？', function (r) {
                if (r) {
                    $.get("/accident/changeClaimState?claimState=" + claimState, {id: row.id}, function (data) {
                        if (data.succeed) {
                            $.messager.alert("温馨提示", "操作成功");
                            accident_datagridYES.datagrid("load");
                        } else {
                            $.messager.alert("温馨提示", data.errorMsg);
                        }
                    })
                }
            });
        },
        // rui:修改状态:END

        // 刷新页面
        reload: function () {
            accident_datagridNOT.datagrid("load");
        },
        reload2: function () {
            accident_datagridYES.datagrid("load");
        },

        // 理赔高级查询
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue");
            var endDate = $("#endDate").textbox("getValue");
            accident_datagridNOT.datagrid("load", {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate
            });
        },

        // 对话框保存信息
        save: function () {
            var id = $("#claimId").val();
            var url = "/accident/save";
            if (id) {
                url = "/accident/update";
            }
            var grid = accident_combogrid.combogrid('grid');	// 获取数据表格对象
            var row = grid.datagrid('getSelected');	// 获取选择的行
            var iddata = row.id;
            accident_combogrid.combogrid("setValue", iddata);
            accident_form.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.succeed) {
                        $.messager.alert("温馨提示", "保存成功");
                        accident_dialog.dialog("close");
                        accident_datagridNOT.datagrid("load");
                    } else {
                        $.messager.alert("温馨提示", data.errorMsg);
                    }
                }
            })
        },

        // 对话框关闭
        cancel: function () {
            accident_dialog.dialog("close");
        },


        // 核价核损对话框保存信息
        save2: function () {
            //test
            var grid = insure_combogrid.combogrid('grid');	// 获取数据表格对象
            var row = grid.datagrid('getSelected');	// 获取选择的行
            console.log("row666");
            console.log(accident_datagridYES.datagrid('getSelected'));
            var accident_datagridYES_data =accident_datagridYES.datagrid('getSelected');
            //console.log(row.id);
          $("#insurerId").val(insurerId);
            //带上accidentId赋值
            console.log(accident_datagridYES_data.id);
            $("#accidentId").val(accident_datagridYES_data.id);
            accident_form2.form("submit", {
                url: "/claimresult/save",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.succeed) {
                        $.messager.alert("温馨提示", "保存成功");
                        accident_dialog2.dialog("close");
                        accident_datagridYES.datagrid("load");
                    } else {
                        $.messager.alert("温馨提示", data.errorMsg);
                    }
                }
            })

        },

        // 核价核损对话框关闭
        cancel2: function () {
            accident_dialog2.dialog("close");
        },
        //核价核损功能
        verifyprice: function () {
            var row = accident_datagridYES.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要操作的行");
                return;
            }
            insurerId = row.claim.id;
            insure_combogrid.combogrid({
                url: '/charge/queryInsureInfoByClientId?clientId=' + insurerId
            })

            // console.log(insure_combogrid.combogrid('options'));
            // console.log(insure_combogrid.combogrid('grid').datagrid('options'));
            //  insure_combogrid.combogrid('grid').datagrid('options').url = '/charge/queryInsureInfoByClientId?clientId=' + insurerId;
            accident_dialog2.dialog("open");
            accident_dialog2.dialog("setTitle", "编辑赔偿明细");
            accident_form2.form("clear");
            insure_combogrid.combogrid('grid').datagrid('reload');
            accident_form2.form("load", '/claimresult/getDetailByInsurerId?insurerId=' + insurerId);
            $("#dialog_btn2").show();
            insure_combogrid.combogrid('readonly', false);
            $("#beneficiary").datebox('readonly', false);
            $("#receivableAccount").textbox('readonly', false);
            $("#money").numberbox('readonly', false);

            $.post('/claimresult/getDetailByInsurerId?insurerId=' + insurerId, function (data) {
                insure_combogrid.combogrid('setValue', data.insurancebill.oddNumber);
            })
        },
        // 核价核损功能:END

        // 查看核价
        verifydetail: function () {
            var row = accident_datagridYES.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要操作的行");
                return;
            }
            insurerId = row.claim.id;
            accident_dialog2.dialog("open");
            accident_dialog2.dialog("setTitle", "查看赔偿明细");
            accident_form2.form("clear");
            accident_form2.form("load", '/claimresult/getDetailByInsurerId?insurerId=' + insurerId);
            $("#dialog_btn2").hide();
            insure_combogrid.combogrid('readonly', true);
            $("#beneficiary").datebox('readonly', true);
            $("#receivableAccount").textbox('readonly', true);
            $("#money").numberbox('readonly', true);
            $.post('/claimresult/getDetailByInsurerId?insurerId=' + insurerId, function (data) {
                insure_combogrid.combogrid('setValue', data.insurancebill.oddNumber);
            })

        },
        // 查看核价:END

        // 查看案件详情
        casedetail: function () {
            var row = accident_datagridYES.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要操作的行");
                return;
            }
            //清除以前的数据
            accident_form.form("clear");
            accident_form.form("load", row);
            accident_dialog.dialog("open");
            accident_dialog.dialog("setTitle", "备案表修改");
            accident_combogrid.combogrid("setValue", row.claim.name);
            $("#dialog_btn").hide();
            accident_combogrid.combogrid('readonly', true);
            $("#accidentTime").datebox('readonly', true);
            $("#accidentPlace").textbox('readonly', true);
            $("#accidentDescription").textbox('readonly', true);
            $("#accidentRemark").textbox('readonly', true);
        }
        // 查看案件详情:END

    };
    // accident_datagridNOT响应函数集合对象:END


    // 调用集合函数
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        claimState = $(this).data("claimstate");
        cmdObj[cmd]();
    });


});




